package xrz.com.jr;

import android.Manifest;
import android.content.Intent;
import android.content.pm.PackageManager;
import android.net.Uri;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.ActivityCompat;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.TextView;

import com.alibaba.fastjson.JSON;

import java.util.List;
import java.util.Map;

import myhttps.Http;
import myhttps.HttpConst;
import myhttps.HttpListener;
import myhttps.HttpUtil;
import xrz.com.jr.adapter.Adapter_TelModel;
import xrz.com.jr.entity.TelModel;
import xrz.com.jr.utils.ExitUtil;

/**
 * Created by Administrator on 2016/7/28.
 */
public class Act_TelModel extends BaseActivity implements AdapterView.OnItemClickListener {
    private ListView listview;
    private Adapter_TelModel adapter;
    private String id;
    private ImageView top_back_img;
    private TextView top_tv;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.layout_nomalmodel);
        listview = (ListView) findViewById(R.id.listview);
        adapter = new Adapter_TelModel(this);
        listview.setAdapter(adapter);
        listview.setOnItemClickListener(this);
        id = getIntent().getStringExtra("id");
        top_tv = (TextView) findViewById(R.id.top_tv);
        top_back_img = (ImageView) findViewById(R.id.top_back_img);
        top_tv.setText(getIntent().getStringExtra("title"));
        top_back_img.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                finish();
            }
        });
        getData();
        ExitUtil.getInstant().addToPool(this);
    }

    private void getData() {
        Map<String, String> map = HttpUtil.getParams("id", id);
        Http.getInstant().sendRequest(this, HttpConst.getTelList, map, new HttpListener() {
            @Override
            public void onHttpSuccess(String result) {
                List<TelModel> list = JSON.parseArray(result, TelModel.class);
                adapter.clear();
                adapter.addAll(list);
            }

            @Override
            public void onHttpError(String error) {

            }
        });
    }

    @Override
    public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) {
        TelModel tel = adapter.getItem(i);
        String t = tel.getTel();
        Intent intent = new Intent(Intent.ACTION_CALL, Uri.parse("tel:" + t));
        if (ActivityCompat.checkSelfPermission(this, Manifest.permission.CALL_PHONE) != PackageManager.PERMISSION_GRANTED) {
            // TODO: Consider calling
            //    ActivityCompat#requestPermissions
            // here to request the missing permissions, and then overriding
            //   public void onRequestPermissionsResult(int requestCode, String[] permissions,
            //                                          int[] grantResults)
            // to handle the case where the user grants the permission. See the documentation
            // for ActivityCompat#requestPermissions for more details.
            return;
        }
        startActivity(intent);
    }
}
